package com.elegantmall.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.elegantmall.pojo.CartPojo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CartDao extends BaseMapper<CartPojo> {

    @Select("SELECT * FROM cart WHERE user_name = #{userName}")
    List<CartPojo> selectByUserName(String userName);

    @Select("SELECT * FROM cart WHERE user_name = #{userName} AND goods_id = #{goodsId}")
    CartPojo selectByUserAndGoods(@Param("userName") String userName, @Param("goodsId") String goodsId);

    @Update("UPDATE cart SET number = #{number} " +
            "WHERE user_name = #{userName} AND goods_id = #{goodsId}")
    int directUpdate(@Param("userName") String userName,
                     @Param("goodsId") String goodsId,
                     @Param("number") Integer number);

    @Insert("INSERT INTO cart (user_name, goods_id, number, price) " +
            "VALUES (#{userName}, #{goodsId}, #{number}, #{price})")
    int customInsert(CartPojo cartItem);

    @Update("UPDATE cart SET number = number + 1 " +
            "WHERE goods_id = #{goodsId}")
    int increaseByOne(@Param("goodsId") String goodsId);

    @Update("UPDATE cart SET number = number - 1 WHERE goods_id = #{goodsId} AND number > 1")
    int decreaseByGoodsId(@Param("goodsId") String goodsId);

    @Delete("DELETE FROM cart WHERE user_name = #{userName} AND goods_id = #{goodsId}")
    int directDelete(@Param("userName") String userName,
                     @Param("goodsId") String goodsId);

    @Delete("DELETE FROM cart WHERE goods_id = #{goodsId} AND number <= 1")
    int deleteWhenLteOne(@Param("goodsId") String goodsId);
}